.class public Lorg/microemu/cldc/datagram/Connection;
.super Ljava/lang/Object;
.source "Connection.java"

# interfaces
.implements Ljavax/microedition/io/DatagramConnection;
.implements Ljavax/microedition/io/UDPDatagramConnection;
.implements Lorg/microemu/microedition/io/ConnectionImplementation;


# static fields
.field public static final PROTOCOL:Ljava/lang/String; = "datagram://"


# instance fields
.field private address:Ljava/lang/String;

.field private socket:Ljava/net/DatagramSocket;


# direct methods
.method public constructor <init>()V
    .locals 0

    .prologue
    .line 41
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method


# virtual methods
.method public close()V
    .locals 1
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 59
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v0}, Ljava/net/DatagramSocket;->close()V

    .line 60
    return-void
.end method

.method public getLocalAddress()Ljava/lang/String;
    .locals 2
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 99
    iget-object v1, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v1}, Ljava/net/DatagramSocket;->getInetAddress()Ljava/net/InetAddress;

    move-result-object v0

    .line 100
    .local v0, address:Ljava/net/InetAddress;
    if-nez v0, :cond_0

    .line 105
    invoke-static {}, Ljava/net/InetAddress;->getLocalHost()Ljava/net/InetAddress;

    move-result-object v0

    .line 112
    :goto_0
    invoke-virtual {v0}, Ljava/net/InetAddress;->getHostAddress()Ljava/lang/String;

    move-result-object v1

    return-object v1

    .line 110
    :cond_0
    iget-object v1, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v1}, Ljava/net/DatagramSocket;->getLocalAddress()Ljava/net/InetAddress;

    move-result-object v0

    .restart local v0       #address:Ljava/net/InetAddress;
    goto :goto_0
.end method

.method public getLocalPort()I
    .locals 1
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 116
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v0}, Ljava/net/DatagramSocket;->getLocalPort()I

    move-result v0

    return v0
.end method

.method public getMaximumLength()I
    .locals 2
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 63
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v0}, Ljava/net/DatagramSocket;->getReceiveBufferSize()I

    move-result v0

    iget-object v1, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-virtual {v1}, Ljava/net/DatagramSocket;->getSendBufferSize()I

    move-result v1

    invoke-static {v0, v1}, Ljava/lang/Math;->min(II)I

    move-result v0

    return v0
.end method

.method public getNominalLength()I
    .locals 1
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 67
    invoke-virtual {p0}, Lorg/microemu/cldc/datagram/Connection;->getMaximumLength()I

    move-result v0

    return v0
.end method

.method public newDatagram(I)Ljavax/microedition/io/Datagram;
    .locals 1
    .parameter "size"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 79
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    invoke-virtual {p0, p1, v0}, Lorg/microemu/cldc/datagram/Connection;->newDatagram(ILjava/lang/String;)Ljavax/microedition/io/Datagram;

    move-result-object v0

    return-object v0
.end method

.method public newDatagram(ILjava/lang/String;)Ljavax/microedition/io/Datagram;
    .locals 1
    .parameter "size"
    .parameter "addr"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 83
    new-instance v0, Lorg/microemu/cldc/datagram/DatagramImpl;

    invoke-direct {v0, p1}, Lorg/microemu/cldc/datagram/DatagramImpl;-><init>(I)V

    .line 84
    .local v0, datagram:Ljavax/microedition/io/Datagram;
    invoke-interface {v0, p2}, Ljavax/microedition/io/Datagram;->setAddress(Ljava/lang/String;)V

    .line 85
    return-object v0
.end method

.method public newDatagram([BI)Ljavax/microedition/io/Datagram;
    .locals 1
    .parameter "buf"
    .parameter "size"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 89
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    invoke-virtual {p0, p1, p2, v0}, Lorg/microemu/cldc/datagram/Connection;->newDatagram([BILjava/lang/String;)Ljavax/microedition/io/Datagram;

    move-result-object v0

    return-object v0
.end method

.method public newDatagram([BILjava/lang/String;)Ljavax/microedition/io/Datagram;
    .locals 1
    .parameter "buf"
    .parameter "size"
    .parameter "addr"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 93
    new-instance v0, Lorg/microemu/cldc/datagram/DatagramImpl;

    invoke-direct {v0, p1, p2}, Lorg/microemu/cldc/datagram/DatagramImpl;-><init>([BI)V

    .line 94
    .local v0, datagram:Ljavax/microedition/io/Datagram;
    invoke-interface {v0, p3}, Ljavax/microedition/io/Datagram;->setAddress(Ljava/lang/String;)V

    .line 95
    return-object v0
.end method

.method public openConnection(Ljava/lang/String;IZ)Ljavax/microedition/io/Connection;
    .locals 6
    .parameter "name"
    .parameter "mode"
    .parameter "timeouts"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    const-string v4, "datagram://"

    .line 120
    invoke-static {}, Lorg/microemu/cldc/http/Connection;->isAllowNetworkConnection()Z

    move-result v3

    if-nez v3, :cond_0

    .line 121
    new-instance v3, Ljava/io/IOException;

    const-string v4, "No network"

    invoke-direct {v3, v4}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v3

    .line 123
    :cond_0
    const-string v3, "datagram://"

    invoke-virtual {p1, v4}, Ljava/lang/String;->startsWith(Ljava/lang/String;)Z

    move-result v3

    if-nez v3, :cond_1

    .line 124
    new-instance v3, Ljava/io/IOException;

    new-instance v4, Ljava/lang/StringBuffer;

    invoke-direct {v4}, Ljava/lang/StringBuffer;-><init>()V

    const-string v5, "Invalid Protocol "

    invoke-virtual {v4, v5}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v4

    invoke-virtual {v4, p1}, Ljava/lang/StringBuffer;->append(Ljava/lang/String;)Ljava/lang/StringBuffer;

    move-result-object v4

    invoke-virtual {v4}, Ljava/lang/StringBuffer;->toString()Ljava/lang/String;

    move-result-object v4

    invoke-direct {v3, v4}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v3

    .line 127
    :cond_1
    const-string v3, "datagram://"

    invoke-virtual {v4}, Ljava/lang/String;->length()I

    move-result v3

    invoke-virtual {p1, v3}, Ljava/lang/String;->substring(I)Ljava/lang/String;

    move-result-object v3

    iput-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    .line 128
    const/4 v2, -0x1

    .line 129
    .local v2, port:I
    iget-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    const/16 v4, 0x3a

    invoke-virtual {v3, v4}, Ljava/lang/String;->indexOf(I)I

    move-result v1

    .line 130
    .local v1, index:I
    const/4 v3, -0x1

    if-ne v1, v3, :cond_2

    .line 131
    new-instance v3, Ljava/io/IOException;

    const-string v4, "port missing"

    invoke-direct {v3, v4}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v3

    .line 133
    :cond_2
    iget-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    add-int/lit8 v4, v1, 0x1

    invoke-virtual {v3, v4}, Ljava/lang/String;->substring(I)Ljava/lang/String;

    move-result-object v3

    invoke-static {v3}, Ljava/lang/Integer;->parseInt(Ljava/lang/String;)I

    move-result v2

    .line 134
    if-nez v1, :cond_3

    .line 136
    new-instance v3, Ljava/net/DatagramSocket;

    invoke-direct {v3, v2}, Ljava/net/DatagramSocket;-><init>(I)V

    iput-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    .line 143
    :goto_0
    return-object p0

    .line 139
    :cond_3
    iget-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->address:Ljava/lang/String;

    const/4 v4, 0x0

    invoke-virtual {v3, v4, v1}, Ljava/lang/String;->substring(II)Ljava/lang/String;

    move-result-object v0

    .line 140
    .local v0, host:Ljava/lang/String;
    new-instance v3, Ljava/net/DatagramSocket;

    invoke-direct {v3}, Ljava/net/DatagramSocket;-><init>()V

    iput-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    .line 141
    iget-object v3, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    invoke-static {v0}, Ljava/net/InetAddress;->getByName(Ljava/lang/String;)Ljava/net/InetAddress;

    move-result-object v4

    invoke-virtual {v3, v4, v2}, Ljava/net/DatagramSocket;->connect(Ljava/net/InetAddress;I)V

    goto :goto_0
.end method

.method public receive(Ljavax/microedition/io/Datagram;)V
    .locals 2
    .parameter "dgram"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 75
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    check-cast p1, Lorg/microemu/cldc/datagram/DatagramImpl;

    .end local p1
    invoke-virtual {p1}, Lorg/microemu/cldc/datagram/DatagramImpl;->getDatagramPacket()Ljava/net/DatagramPacket;

    move-result-object v1

    invoke-virtual {v0, v1}, Ljava/net/DatagramSocket;->receive(Ljava/net/DatagramPacket;)V

    .line 76
    return-void
.end method

.method public send(Ljavax/microedition/io/Datagram;)V
    .locals 2
    .parameter "dgram"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 71
    iget-object v0, p0, Lorg/microemu/cldc/datagram/Connection;->socket:Ljava/net/DatagramSocket;

    check-cast p1, Lorg/microemu/cldc/datagram/DatagramImpl;

    .end local p1
    invoke-virtual {p1}, Lorg/microemu/cldc/datagram/DatagramImpl;->getDatagramPacket()Ljava/net/DatagramPacket;

    move-result-object v1

    invoke-virtual {v0, v1}, Ljava/net/DatagramSocket;->send(Ljava/net/DatagramPacket;)V

    .line 72
    return-void
.end method
